import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ShoppingCartActivity extends Activity {

    private ListView cartListView;
    private TextView totalPriceTextView;
    private Button clearCartButton;

    private List<CartItem> cartItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shopping_cart);

        cartListView = findViewById(R.id.cartListView);
        totalPriceTextView = findViewById(R.id.totalPriceTextView);
        clearCartButton = findViewById(R.id.clearCartButton);

        cartItems = new ArrayList<>();

        // 添加一些示例商品到购物车
        cartItems.add(new CartItem("苹果", 5.0));
        cartItems.add(new CartItem("香蕉", 3.0));

        CartAdapter adapter = new CartAdapter(this, cartItems);
        cartListView.setAdapter(adapter);

        calculateTotalPrice();

        clearCartButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                cartItems.clear();
                adapter.notifyDataSetChanged();
                totalPriceTextView.setText("0.0");
            }
        });
    }

    private void calculateTotalPrice() {
        double total = 0;
        for (CartItem item : cartItems) {
            total += item.getPrice();
        }
        totalPriceTextView.setText(String.format("%.2f", total));
    }

    class CartItem {
        private String name;
        private double price;

        public CartItem(String name, double price) {
            this.name = name;
            this.price = price;
        }

        public String getName() {
            return name;
        }

        public double getPrice() {
            return price;
        }
    }

    class CartAdapter extends BaseAdapter {

        private Activity context;
        private List<CartItem> items;

        public CartAdapter(Activity context, List<CartItem> items) {
            this.context = context;
            this.items = items;
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = context.getLayoutInflater().inflate(R.layout.cart_item, null);
            }

            TextView nameTextView = convertView.findViewById(R.id.nameTextView);
            TextView priceTextView = convertView.findViewById(R.id.priceTextView);

            CartItem item = items.get(position);

            nameTextView.setText(item.getName());
            priceTextView.setText(String.format("%.2f", item.getPrice()));

            return convertView;
        }
    }
}
